package com.rajaev.dtu.tractor.domain.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rajaev.dtu.tractor.app.service.FaultService;
import com.rajaev.dtu.tractor.domain.entity.BatteryPo;
import com.rajaev.dtu.tractor.domain.entity.UserInfo;
import com.rajaev.dtu.tractor.domain.repository.mysql.BatteryDao;
import com.rajaev.dtu.tractor.domain.repository.mysql.FaultDao;
import com.rajaev.dtu.tractor.domain.repository.mysql.UserDao;
import com.rajaev.dtu.tractor.domain.repository.redis.DeviceMonitorRedisRepository;
import com.rajaev.dtu.tractor.domain.vo.*;
import com.rajaev.dtu.tractor.infrastructure.base.enums.BatteryOnlineStatus;
import com.rajaev.dtu.tractor.infrastructure.helper.UserSecurityUtil;
import com.rajaev.dtu.tractor.infrastructure.security.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Created with IDEA
 * @author: tianjie
 * @Date:2019/4/8
 * @Time:17:40
 */
@Service
@Slf4j
public class FaultServiceImpl implements FaultService {


    @Autowired
    FaultDao faultDao;

    @Autowired
    UserDao userDao;

    @Autowired
    BatteryDao batteryDao;

    @Autowired
    DeviceMonitorRedisRepository deviceMonitorRedisRepository;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserSecurityUtil userSecurityUtil;

    @Override
    public PageInfo<FaultVo> selectFaults(HttpServletRequest request, Integer pageSize, Integer pageNum, String vin) {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();

        PageInfo<FaultVo> faultVo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> faultDao.selectFaults(vin, userInfo.getId()));
        return faultVo;
    }


    public List<FaultDetailVo> selectAllNotOveredFault(){
        return faultDao.selectAllNotOveredFaults();
    }


    @Override
    public List<FaultDetailVo> selectNotOveredFault(String vin) {
        return faultDao.selectAllNotOveredFaultsByVin(vin);
    }

    public void addFaultRecord(Integer batteryId, int  faultCode, Date happenTime){
        FaultDetailVo faultDetailVo = new FaultDetailVo();
        faultDetailVo.setBatteryId(batteryId);
        faultDetailVo.setFaultCode(faultCode);
        faultDetailVo.setStartTime(happenTime);
        faultDao.insertFault(faultDetailVo);
    }


    @Override
    public BatteryFaultStatisticVo queryBatteryFaultStatisticVo(Integer userId, Date searchBegin, Date searchEnd) {
        List<FaultDetailVo> list = faultDao.selectFaultDetailsByUserId(userId, searchBegin, searchEnd);
        long level1 = list.stream().filter(faultDetailVo -> faultDetailVo.getFaultLevel() == 1).count();
        long level2 = list.stream().filter(faultDetailVo -> faultDetailVo.getFaultLevel() == 2).count();
        BatteryFaultStatisticVo batteryFaultStatisticVo = new BatteryFaultStatisticVo();
        batteryFaultStatisticVo.setLevel1FaultNum((int) level1);
        batteryFaultStatisticVo.setLevel2FaultNum((int) level2);
        List<BatteryPo> userBatteryList = batteryDao.selectUserBattery(userId);

        long offlineCount = userBatteryList.stream().filter(batteryPo -> BatteryOnlineStatus.ONLINE.equals(batteryPo.getOnlineStatus())).count();
        int batteryNumInFault = userBatteryList.size() - batteryDao.selectBatteryInFault(userId).size();

        batteryFaultStatisticVo.setOfflineNum((int) offlineCount);
        batteryFaultStatisticVo.setNormalFaultNum(batteryNumInFault);
        return batteryFaultStatisticVo;
    }

    @Override
    public Integer selectFaultNumber(Integer userId) {
        return faultDao.selectFaultNumberWithUserId(userId);
    }

    @Override
    public PageInfo<BatteryFaultWarmInfo> selectBatteryWarnInfos(HttpServletRequest request,Integer pageSize, Integer pageNum) {
        //从redis中获取故障信息
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();

        //查询用户可以看到的所有的电池信息
        PageInfo<BatteryFaultWarmInfo> batterList = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> faultDao.selectBatterWarnInfo(userInfo.getId()));

        return batterList;
    }


    @Override
    public void setFaultReset(List<FaultDetailVo> faultDetailVos) {
        faultDao.updateFaultReset(faultDetailVos);
    }

    /**
     * 根据vin查询单个电池的故障详情列表
     *
     * @param pageSize 每页条数
     * @param pageNo   页号
     * @param vin      vin
     * @return
     */
    @Override
    public PageInfo<FaultDetailVo> selectFaultDetail(Integer pageSize,
                                                     Integer pageNo,
                                                     String vinOrPlateNo,
                                                     Integer faultLevel,
                                                     Date searchBegin, Date searchEnd) {
        return PageHelper.startPage(pageNo, pageSize)
                .doSelectPageInfo(() -> faultDao.selectFaultDetailByVin(vinOrPlateNo, faultLevel, searchBegin, searchEnd));
    }

    @Override
    public PageInfo<FaultItemVo> pageQueryFaultItems(Integer pageSize, Integer pageNum, String faultDesc, String faultCode) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> faultDao.selectFaultItem(faultCode, faultDesc));
    }

    @Override
    public void updateFaultItem(FaultItemVo faultItemVo) {
        faultDao.updateFaultItem(faultItemVo);
    }
}
